// 定义数据格式
var app = getApp();
var phone_number;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showModal:{
      type:Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    input_value:'',
   
  },

  /**
   *
   */
  methods: {
    //隐藏模态框
    hideModal:function(){
      this.triggerEvent('hideModal');
    },
    //输入款输入完成
    bindReplaceInput:function(e){
      console.log(e.detail.value, '输入');
      phone_number = e.detail.value;
    },
    //绑定按钮
    bind:function(){
      var that = this;
      console.log('绑定手机号码', phone_number);

      if (phone_number.length<11)return;
      
      //校正电话号码
      var result = this.checkPhone(phone_number);
      console.log(result);
      if (result == false)return;
      
        app.apiPost(app.apiList.bind_phone, {
          content: {
            phoneNumber: phone_number
          }
        }, function (res) {
          console.log(res, '绑定电话');

          //绑定成功，改成true，关闭模态
          app.globalData.binding_phone = true;

          that.triggerEvent('hideModal');

          that.triggerEvent('phone_call', app.globalData.binding_phone);
        })
    },
    /**
     *校对电话号码 
     */
    checkPhone: function (mobile){
      var phone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
      if (mobile.length == 11) {//手机号码
        if (phone.test(mobile)) {
          console.log(mobile);
          return true;
        }
      }
      app.alert('请输入正确的手机格式');
      return false;
    }
    
  }
})